package com.zhentao.login.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhentao.login.mapper.TRoleMapper;
import com.zhentao.login.service.TRoleService;
import com.zhentao.nlz.project.login.TRole;
import com.zhentao.nlz.project.product.vo.RoleVo;
import com.zhentao.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import com.zhentao.nlz.project.product.vo.RoleVo;
import com.zhentao.nlz.project.product.vo.UserVo;
import com.zhentao.utils.Result;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
* @author 不.凌
* @description 针对表【t_role(角色表)】的数据库操作Service实现
* @createDate 2024-09-22 20:51:43
*/
@Service
public class TRoleServiceImpl extends ServiceImpl<TRoleMapper, TRole>
    implements TRoleService{
@Autowired
private TRoleMapper roleMapper;
    @Override
    public Result getrole(RoleVo roleVo) {
        // 创建分页对象
        IPage<RoleVo> page = new Page<>(roleVo.getPageNum(), roleVo.getPageSize());

        // 创建 LambdaQueryWrapper 来构造查询条件
        LambdaQueryWrapper<TRole> wrapper = new LambdaQueryWrapper<TRole>();
        if (roleVo.getRoleName()!= null &&!roleVo.getRoleName().isEmpty()) {
            wrapper.like(TRole::getRoleName, roleVo.getRoleName());
        }
        // 假设还有其他可能的查询条件可以在这里添加

        // 执行查询并获取结果列表
        List<TRole> userList = roleMapper.selectList(wrapper);

        // 将数据库实体列表转换为 RoleVo 列表
        List<RoleVo> roleVoList = new ArrayList<>();
        for (TRole role : userList) {
            RoleVo vo = new RoleVo();
            // 可以根据实际情况进行更精细的属性复制
            BeanUtils.copyProperties(role, vo);
            roleVoList.add(vo);
        }

        // 返回用户列表
        return Result.success(roleVoList);
    }



    @Override
    public boolean removeRoleId(Integer id) {
        boolean remove = this.removeById(id);
        return remove;
    }

    @Override
    public Result getrolelist() {
        LambdaQueryWrapper<TRole> wrapper = new LambdaQueryWrapper<>();

        List<TRole> roleList = roleMapper.selectList(wrapper);
        return Result.success(roleList);
    }

    @Override
    public boolean saverole(TRole tRole) {
        tRole.setCreateTime(new Date());
        System.err.println(tRole+"123445414412");
        int insert = roleMapper.insert(tRole);
        return true;
    }

    @Override
    public boolean updaterole(TRole role) {
        role.setUpdateTime(new Date());
        int insert = roleMapper.updateById(role);
        return true;
    }


    @Override
    public Result getRoleId(Integer id) {
        TRole tRole = roleMapper.selectById(id);
        return Result.success(tRole);
    }
}